﻿//-----------------------------------------------------------------------
// <copyright file="GoatFieldSize.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGen.Object
{
    using System;

    /// <summary>
    /// Size field.
    /// </summary>
    public class GoatFieldSize : GoatField
    {
        #region Constants - Private

        /// <summary>
        /// Array size.
        /// </summary>
        private const int ArraySize = 2;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for a field.
        /// </summary>
        /// <param name="name">Name of the field.</param>
        /// <param name="properties">Properties for the field. Min, max, etc. Each field type defines
        /// the properties that it supports.</param>
        public GoatFieldSize(string name, GoatFieldProperties properties) :
            base(name, properties)
        {
            this.ParseDefault();
            this.ParseMin();
            this.ParseMax();
            this.ClampDefault();
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets if the min size is valid.
        /// </summary>
        public bool MinSizeValid
        {
            get
            {
                return this.MinSize != null;
            }
        }

        /// <summary>
        /// Gets if the max size is valid.
        /// </summary>
        public bool MaxSizeValid
        {
            get
            {
                return this.MaxSize != null;
            }
        }

        /// <summary>
        /// Gets the min size.
        /// </summary>
        public float[] MinSize
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the max size.
        /// </summary>
        public float[] MaxSize
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the default value.
        /// </summary>
        public float[] Default
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the C# type of the field.
        /// </summary>
        public override Type Type
        {
            get
            {
                return typeof(float[]);
            }
        }

        #endregion

        #region Fields - Private

        /// <summary>
        /// Parse the default value.
        /// </summary>
        private void ParseDefault()
        {
            float[] value = this.ParseFloatArray(GoatFieldConstants.DefaultKey, ArraySize, 0.0f);
            if (value != null)
            {
                this.Default = value;
                return;
            }

            this.Default = new float[ArraySize];

            this.Default[0] = 0.0f;
            this.Default[1] = 0.0f;
        }

        /// <summary>
        /// Parse the min value.
        /// </summary>
        private void ParseMin()
        {
            float[] value = this.ParseFloatArray(GoatFieldConstants.MinKey, ArraySize, 0.0f);
            if (value != null)
            {
                this.MinSize = value;
                return;
            }

            this.MinSize = null;
        }

        /// <summary>
        /// Parse the max value.
        /// </summary>
        private void ParseMax()
        {
            float[] value = this.ParseFloatArray(GoatFieldConstants.MaxKey, ArraySize, 0.0f);
            if (value != null)
            {
                this.MaxSize = value;
                return;
            }

            this.MaxSize = null;
        }

        /// <summary>
        /// Clamp the defalut value to min / max.
        /// </summary>
        private void ClampDefault()
        {
            if (this.MinSize != null)
            {
                if (this.Default[0] < this.MinSize[0])
                {
                    this.Default[0] = this.MinSize[0];
                }

                if (this.Default[1] < this.MinSize[1])
                {
                    this.Default[1] = this.MinSize[1];
                }
            }

            if (this.MaxSize != null)
            {
                if (this.Default[0] > this.MaxSize[0])
                {
                    this.Default[0] = this.MaxSize[0];
                }

                if (this.Default[1] > this.MaxSize[1])
                {
                    this.Default[1] = this.MaxSize[1];
                }
            }
        }

        #endregion
    }
}